package com.zhaoc.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhaoc.domain.Standard;
import com.zhaoc.service.IStandardService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ParentPackage("json-default")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{

	private Standard standard = new Standard();
	
	@Autowired
	private IStandardService standardService;
	
	@Override
	public Standard getModel() {
		return standard;
	}
	
	//添加收派标准
	@Action(value="standard_save",
			results= {@Result(name="success",
					type="redirect",
				location="./pages/base/standard.html"
				)})
	public String save() {
		standardService.save(standard);
		return SUCCESS;
	}

	private int page;

	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	//分页查询收派标准
	@Action(value = "standard_pageQuery",results = {@Result(name = SUCCESS,type = "json")})
	public String pageQuery(){
		//调用业务层查询数据结果
		Pageable pageable = new PageRequest(page -1,rows);
		Page<Standard> pageData = standardService.findPageData(pageable);

		//返回客户端数据需要total和rows
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("total",pageData.getNumberOfElements());
		result.put("rows",pageData.getContent());

		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	//查询所有的收派标准
	@Action(value = "standard_findAll",results = {@Result(name = "success",type = "json")})
	public String findAllStandard(){
		List<Standard> list = standardService.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
}
